package appbase.jfinal.ext;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import appbase.exception.BizzException;


public class ApiBaseController extends BaseController{

	public static final String ATTR_MOBILE_PARAM_MAP = "api_param_map";
	public static final String PARAM_API_KEY_USER_ID = "api_user_id";
	public static final String PARAM_API_KEY_TOKEN = "api_token";
	
	/**
	 * 读取移动端传输的userId
	 */
	public int getApiUserId() 
	{
		Integer userId = getParaToInt(PARAM_API_KEY_USER_ID);
		if (userId == null || userId <= 0) 
		{
			return 0;
		}
		return userId;
		//return 1;
	}
	
	public boolean isApiLogin() 
	{
		return getApiUserId() > 0;
	}
	
	public static boolean isApiLogin(int apiUserId)
	{
		return apiUserId > 0;
	}
	
	/*
	 ********************************
	 *****  从 body 流里获取参数  *****
	 ********************************
	*/
	
	@SuppressWarnings("unchecked")
	public Object getBodyParam(String name)
	{
		Map<String, Object> paramMap =(Map<String, Object>)getRequest().getAttribute(ATTR_MOBILE_PARAM_MAP);
		return paramMap.get(name);
	}
	
	@Override
	public String getPara(String name) 
	{
		Object value = getBodyParam(name);
		if (value != null && value instanceof String) 
		{
			return (String)value;
		}
		return value == null ? null : value.toString();
	}
	
	@Override
	public String getPara(String name, String defaultValue) 
	{
		String result = getPara(name);
		return result != null && !"".equals(result) ? result : defaultValue;
	}
	
	@Override
	public Integer getParaToInt(String name) 
	{
		Object value = getBodyParam(name);
		if (value != null && value instanceof Integer) 
		{
			return (Integer)value;
		}
		return (value == null || value.toString().isEmpty()) ? null : Integer.parseInt(value.toString()); 
	}
	
	@Override
	public Integer getParaToInt(String name, Integer defaultValue) 
	{
		Integer value = getParaToInt(name);
		return value == null ? defaultValue : value; 
	}
	
	@Override
	public Long getParaToLong(String name) 
	{
		Object value = getBodyParam(name);
		if (value != null && value instanceof Long) 
		{
			return (Long)value;
		}
		return value == null ? null : Long.parseLong(value.toString());
	}
	
	@Override
	public Long getParaToLong(String name, Long defaultValue) 
	{
		Long value = getParaToLong(name);
		return value == null ? defaultValue : value;
	}
	
	@Override
	public Boolean getParaToBoolean(String name, Boolean defaultValue) 
	{
		Boolean value = getParaToBoolean(name);
		return value == null ? defaultValue : value;
	}
	
	@Override
	public Boolean getParaToBoolean(String name) 
	{
		Object value = getBodyParam(name);
		if (value == null)
		{
			return null;
		}
		
		if (value instanceof Boolean) 
		{
			return (Boolean)value;
		}
		
		String result = value.toString().trim().toLowerCase();
		if ("1".equals(result) || "true".equals(result))
		{
			return Boolean.TRUE;
		}else if ("0".equals(result) || "false".equals(result))
		{
			return Boolean.FALSE;
		}
		throw new RuntimeException("Can not parse the parameter \"" + result + "\" to boolean value.");
	}
	
	@Override
	public Date getParaToDate(String name) 
	{
		Object value = getBodyParam(name);
		if (value == null || value.toString().trim().length() <= 0) 
		{
			return null;
		}
		try {
			return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(value.toString());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public Date getParaToDate(String name, Date defaultValue) 
	{
		Date value = getParaToDate(name);
		return value == null ? null : value;
	}
	
	
	/*
	 ********************************
	 *****  输出加密的 json 串  ******
	 ********************************
	*/
	
	public void renderJson(String key, Object value) {
		render(new EncryptJsonRender(key, value));
	}
	
	
	public void renderJson() {
		render(new EncryptJsonRender());
	}
	
	
	public void renderJson(String[] attrs) {
		render(new EncryptJsonRender(attrs));
	}
	
	
	public void renderJson(String jsonText) {
		render(new EncryptJsonRender(jsonText));
	}
	
	
	public void renderJson(Object object) {
		render(new EncryptJsonRender(object));
	}
	
	public void handleExpWithTx(Exception e) 
	{
		if (!(e instanceof BizzException)) 
		{
			e.printStackTrace();
		}
		renderFail(e.getMessage());
		throw new RuntimeException(e.getMessage());
	}
	
	public void handleExp(Exception e) 
	{
		if (!(e instanceof BizzException)) 
		{
			e.printStackTrace();
		}
		renderFail(e.getMessage());
	}
	
}
